const formatJSON = (str) => {
  if (typeof str === 'object') return str;
  try {
    return JSON.parse(str);
  } catch (e) {
    console.warn(str, e);
    return '';
  }
};

const checkInfoItemErr = (act, keys = []) => {
  try {
    keys.forEach((key) => {
      if (!act[key] || !Object.keys(act[key]).length) {
        throw Object.assign({}, { msg: `${act.id}-${act.type} info 缺少参数 ${key}` });
      }
    });
    return undefined;
  } catch (err) {
    const errMsg = err.msg;
    console.warn(errMsg);
    return errMsg;
  }
};

const formatCreatedSpace = (act) => {
  const err = checkInfoItemErr(act, ['space']);
  if (err) return { err: `${act.id}-${act.type} ${err.msg}` };
  return {
    desc: '创建了空间',
    title: act.space.title,
    title_path: `/spaces/${act.space.id}`,
    // user 当前查看的用户
  };
};

const formatCreatedContribution = (act) => {
  const err = checkInfoItemErr(act, ['space', 'contribution']);
  if (err) return { err };
  return {
    desc: '提交了贡献',
    title: act.contribution.title,
    title_path: `/spaces/${act.space.id}/contributions/${act.contribution.id}`,
    // user 当前查看的用户
  };
};

const formatCreatedIssue = (act) => {
  const err = checkInfoItemErr(act, ['space', 'issue']);
  if (err) return { err };
  return {
    desc: '提出了问题',
    title: act.issue.title,
    title_path: `/spaces/${act.space.id}/issues/${act.issue.id}`,
    content: act.issue.content,
    content_path: `/spaces/${act.space.id}/issues/${act.issue.id}`,
    user: formatJSON(act.issue.author),
  };
};

const formatUpdatedIssue = (act) => {
  const err = checkInfoItemErr(act, ['space', 'issue']);
  if (err) return { err };
  return {
    desc: '编辑了问题',
    title: act.issue.title,
    title_path: `/spaces/${act.space.id}/issues/${act.issue.id}`,
    content: act.issue.content,
    content_path: `/spaces/${act.space.id}/issues/${act.issue.id}`,
    user: formatJSON(act.issue.author),
  };
};

const formatCommentedDoc = (act) => {
  const err = checkInfoItemErr(act, ['space', 'doc', 'comment']);
  if (err) return { err };
  return {
    desc: '跟帖了资源',
    title: act.doc.title,
    title_path: `/spaces/${act.space.id}/resources-docs/${act.doc.id}`,
    content: act.comment.content,
    content_path: `/spaces/${act.space.id}/resources-docs/${act.doc.id}?page=${parseInt(act.comment.floor / 10, 10) + 1}&comment_id=${act.comment.id}`,
    // user 当前查看的用户
  };
};

const formatCommentedContrib = (act) => {
  const err = checkInfoItemErr(act, ['space', 'contribution', 'comment']);
  if (err) return { err };
  return {
    desc: '评论了贡献',
    title: act.contribution.title,
    title_path: `/spaces/${act.space.id}/contributions/${act.contribution.id}`,
    content: act.comment.content,
    content_path: `/spaces/${act.space.id}/contributions/${act.contribution.id}?page=${parseInt(act.comment.floor / 10, 10) + 1}&comment_id=${act.comment.id}`,
    // user 当前查看的用户
  };
};

const formatAnsweredIssue = (act) => {
  const err = checkInfoItemErr(act, ['space', 'issue', 'comment']);
  if (err) return { err };
  return {
    desc: '回答了问题',
    title: act.issue.title,
    title_path: `/spaces/${act.space.id}/issues/${act.issue.id}`,
    content: act.comment.content,
    content_path: `/spaces/${act.space.id}/issues/${act.issue.id}?page=${parseInt(act.comment.floor / 10, 10) + 1}&comment_id=${act.comment.id}`,
    // user 当前查看的用户
  };
};

const formatLikedSpace = (act) => {
  const err = checkInfoItemErr(act, ['space']);
  if (err) return { err };
  return {
    desc: '点赞了空间',
    title: act.space.title,
    title_path: `/spaces/${act.space.id}`,
    // user 创建空间的用户（不显示）
  };
};

const formatLikedDoc = (act) => {
  const err = checkInfoItemErr(act, ['space', 'doc']);
  if (err) return { err };
  return {
    desc: '点赞了资源',
    title: act.doc.title,
    title_path: `/spaces/${act.space.id}/resources_docs/${act.doc.id}`,
    content: act.doc.content,
    content_path: `/spaces/${act.space.id}/resources_docs/${act.doc.id}`,
    user: formatJSON(act.doc.author),
  };
};

const formatLikedIssue = (act) => {
  const err = checkInfoItemErr(act, ['space', 'issue']);
  if (err) return { err };
  return {
    desc: '点赞了问题',
    title: act.issue.title,
    title_path: `/spaces/${act.space.id}/issues/${act.issue.id}`,
    content: act.issue.content,
    content_path: `/spaces/${act.space.id}/issues/${act.issue.id}`,
    user: formatJSON(act.issue.author),
  };
};

const formatLikedAnswer = (act) => {
  const err = checkInfoItemErr(act, ['space', 'issue', 'comment']);
  if (err) return { err };
  return {
    desc: '点赞了回答',
    title: act.issue.title,
    title_path: `/spaces/${act.space.id}/issues/${act.issue.id}`,
    content: act.comment.content,
    content_path: `/spaces/${act.space.id}/issues/${act.issue.id}?page=${parseInt(act.comment.floor / 10, 10) + 1}&comment_id=${act.comment.id}`,
    user: formatJSON(act.comment.author),
  };
};

const formatFollowedSpace = (act) => {
  const err = checkInfoItemErr(act, ['space']);
  if (err) return { err };
  return {
    desc: '关注了空间',
    title: act.space.title,
    title_path: `/spaces/${act.space.id}`,
    // 不显示 user
  };
};

const formatFollowedUser = (act) => {
  const err = checkInfoItemErr(act, ['user']);
  if (err) return { err };
  return {
    desc: '关注了用户',
    title: act.user.nickname,
    title_path: `/users/${act.user.id}`,
    // 这里可能显示有问题
  };
};

const formatCollectedDoc = (act) => {
  const err = checkInfoItemErr(act, ['space', 'doc']);
  if (err) return { err };
  return {
    desc: '收藏了资源',
    title: act.doc.title,
    title_path: `/spaces/${act.space.id}/resources-docs/${act.doc.id}`,
    content: act.doc.content,
    content_path: `/spaces/${act.space.id}/resources-docs/${act.doc.id}`,
    user: formatJSON(act.doc.author),
  };
};

const formatCollectedIssue = (act) => {
  const err = checkInfoItemErr(act, ['space', 'issue']);
  if (err) return { err };
  return {
    desc: '收藏了问题',
    title: act.issue.title,
    title_path: `/spaces/${act.space.id}/issues/${act.issue.id}`,
    content: act.issue.content,
    content_path: `/spaces/${act.space.id}/issues/${act.issue.id}`,
    user: formatJSON(act.issue.author),
  };
};

const formatCollectedAnswer = (act) => {
  const err = checkInfoItemErr(act, ['space', 'issue', 'comment']);
  if (err) return { err };
  return {
    desc: '收藏了回答',
    title: act.issue.title,
    title_path: `/spaces/${act.space.id}/issues/${act.issue.id}`,
    content: act.comment.content,
    content_path: `/spaces/${act.space.id}/issues/${act.issue.id}?page=${parseInt(act.comment.floor / 10, 10) + 1}&comment_id=${act.comment.id}`,
    user: formatJSON(act.comment.author),
  };
};

export default (act) => {
  const reAct = {
    id: act.id,
    version: act.version,
    type: act.action_type,
    created_at: act.created_at,
    updated_at: act.updated_at,
  };

  if (!act.info || !Object.keys(act.info).length) {
    return Object.assign(reAct, { err: '缺少 info 参数' });
  }
  Object.keys(act.info).forEach((key) => {
    Object.assign(reAct, { [key]: formatJSON(act.info[key]) });
  });

  switch (reAct.type) {
    case 'created_space':
      Object.assign(reAct, formatCreatedSpace(reAct));
      break;
    case 'created_contribution':
      Object.assign(reAct, formatCreatedContribution(reAct));
      break;
    case 'created_issue':
      Object.assign(reAct, formatCreatedIssue(reAct));
      break;
    case 'updated_issue':
      Object.assign(reAct, formatUpdatedIssue(reAct));
      break;
    case 'commented_doc':
      Object.assign(reAct, formatCommentedDoc(reAct));
      break;
    case 'commented_contribution':
      Object.assign(reAct, formatCommentedContrib(reAct));
      break;
    case 'answered_issue':
      Object.assign(reAct, formatAnsweredIssue(reAct));
      break;
    case 'liked_space':
      Object.assign(reAct, formatLikedSpace(reAct));
      break;
    case 'liked_doc':
      Object.assign(reAct, formatLikedDoc(reAct));
      break;
    case 'liked_issue':
      Object.assign(reAct, formatLikedIssue(reAct));
      break;
    case 'liked_answer':
      Object.assign(reAct, formatLikedAnswer(reAct));
      break;
    case 'followed_space':
      Object.assign(reAct, formatFollowedSpace(reAct));
      break;
    case 'followed_user':
      Object.assign(reAct, formatFollowedUser(reAct));
      break;
    case 'collected_doc':
      Object.assign(reAct, formatCollectedDoc(reAct));
      break;
    case 'collected_issue':
      Object.assign(reAct, formatCollectedIssue(reAct));
      break;
    case 'collected_answer':
      Object.assign(reAct, formatCollectedAnswer(reAct));
      break;
    default: break;
  }

  return reAct;
};
